import React, { Component ,Suspense} from 'react'
import {Switch,Route,Redirect} from 'react-router-dom'

export default class RouterView extends Component {
    render() {
        let {routers} =this.props
        let reDom =routers.filter(item=>!item.to)
        let reDir =routers.filter(item=>item.to)
        return (
            <Suspense fallback={<h3>加载中...</h3>}>
                <Switch>
                    {
                        reDom.map( (item,index)=><Route path={item.path} key={index} render={props=>{
                            if(item.children){
                                return <item.component {...props} list={item.children} />
                            }else{
                                return <item.component {...props}  />
                            }
                        }}></Route> )
                    }
                    {
                        reDir.map( (item,index)=><Redirect to={item.to} key={index} from={item.from }  ></Redirect> )
                    }
                </Switch>
            </Suspense>
        )
    }
}
